# *** GLOBALS
conn <- socketConnection(port=8888,blocking=T)
i <- 1
wsize <- 50
fsize <- 6
tgt_pressure <- 40
tgt_length <- 500
exit <- FALSE
hstry <- c()
graphics.off()
quartz()

addEvents <- function(hstry) {
	fnames <- c("time","note","status","pressure")
	l <- unlist(strsplit(readLines(conn,n=1)," "))
	l <- as.integer(l[-2])
	names(l) <- fnames
	if (i==1) {
		hstry <- data.frame(matrix(l,nrow=1))
		colnames(hstry) <- fnames
	} else {
		hstry <- rbind(hstry,l)
	}
	return(hstry)
}

dashboard <- function() {
	source('dbLib.R')
	par(mfrow=c(1,2))
	query <- 'select left(event_tstmp,7) as date_id,count(*) stroke_count, avg(pressure) mean_pressure
from history_d
where status=144
group by date_id
order by date_id'
	result <- fetchResult(query)
	barplot(
			names.arg=result$date_id,
			height=result$stroke_count,
			col='blue',main="Keystrokes per month"
	)
	abline(h=median(result$stroke_count),col='green')
	barplot(
			names.arg=result$date_id,
			height=result$mean_pressure,
			col='blue',main="Mean pressure by month"
			)
	abline(h=median(result$mean_pressure),col='green')
}

# To start:
# 1. Start Java Midi recorder
# 2. Submit code below
# 3. Strike a key
run <- function() {
	glevel <- 1
	while (!exit) {
		hstry <- addEvents(hstry)
		if (l['note']==21) {
			exit <- TRUE
		}
		#***
		if (nrow(hstry) >= wsize) {
			wstart <- nrow(hstry) - wsize + 1
		} else {
			wstart <- 1
		}
		wend <- nrow(hstry)
		h <- hstry[wstart:wend,]
		downs <- h[h$status==144,]
		ups <- h[h$status==128,]
		par(
			mfrow=c(glevel,1),
			bg='black',
			col.axis='white',
			col.lab='white',
			col.main='white',
			col.sub='white'
		)
		if (nrow(downs) == nrow(ups)) {
			ht <- merge(downs,ups,by=c('note'))
			ht <- ht[ht$time.y > ht$time.x,]
			ht <- aggregate(ht,by=list(note=ht$note,time=ht$time.x),FUN=min)[,c('note','time','pressure.x','time.y')]
			ht <- cbind(ht,length=matrix(ht$time.y) - matrix(ht$time))[,-4]
			colnames(ht) <- c('note','time','pressure','length')
			#***
			xfortime <- T
			if (xfortime) {
				x <- ht$time/1000
			} else {
				x <- 1:nrow(ht)
			}
			if (glevel >= 1) {
				if (!exists('mean_pressure')) {
					mean_pressure <- c()
					mean_error <- c()
				}
				mean_pressure[wend-wstart] <- mean(ht$pressure)
				mean_error[wend-wstart] <- mean(ht$pressure - mean(ht$pressure))
				matplot(x=x,
					list(ht$pressure,mean_pressure,mean_error)
					,typ='s',col=2:3,xlab='time',ylab='pressure',ylim=c(0,128),bg=0,axes=F)
				axis(side=1,col='white')
				axis(side=2,col='white')
				abline(h=mean(ht$pressure),col='blue')
			}
			if (glevel >= 2) {
				matplot(x=x,ht$length,typ='s',col=2:3,xlab='time',ylab='length',axes=F)
				axis(side=1,col='white')
				axis(side=2,col='white')
				abline(h=median(ht$length),col='blue')
			}
			if (glevel >= 3) {
				if (wend >= fsize) {
					y <- cbind(h$pressure,filter(h$pressure,filter=rep(1/fsize,fsize),sides=1))
				} else {
					y <- h$pressure
				}	
				plot(x=ht$pressure,y=ht$length,col=c(rep(x='blue',times=49),'red'))
				axis(side=1,col='white')
				axis(side=2,col='white')
				abline(v=median(ht$pressure),col='green')
				abline(h=median(ht$length),col='green')
			}
			if (glevel >= 4) {
				boxplot(x=ht$pressure,col='blue')
			}
			if (glevel >= 5) {
				boxplot(x=ht$length,col='blue')
			}
		}
		i <- i+1
	}
	close(conn)
}

